<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="auto"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('regionLang.regionCode')" prop="regionCode">
            <el-input
              v-model="queryParams.regionCode"
              :placeholder="$t('pleaseEnter')"
              clearable
              filterable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('regionLang.regionName')" prop="regionName">
            <el-input
              v-model="queryParams.regionName"
              :placeholder="$t('pleaseEnter')"
              clearable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotListAllStatusList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            :label="$t('regionLang.regionStatus')"
            prop="regionStatus"
          >
            <el-select
              v-model="queryParams.regionStatus"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in regionStatusOptions"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
        >{{ $t("add") }}</el-button>
      </el-col>
    </el-row>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="100"
      />
      <el-table-column
        :label="$t('regionLang.regionCodeAndName')"
        prop="regionCodeAndName"
        align="center"
        min-width="150"
      />
      <el-table-column
        :label="$t('regionLang.regionStatus')"
        align="center"
        min-width="100"
      >
        <template v-slot="{ row }">
          <el-tag :type="regionStatusTag[row.regionStatus]">
            {{ $t(getTitleByValue(regionStatusOptions, row.regionStatus)) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('regionLang.regionType')"
        prop="regionType"
        align="center"
        min-width="100"
      >
        <template v-slot="{ row }">
          <div>
            {{
              $t(
                "regionLang." +
                  getTitleByValue(depotRegionTypeOptions, row.regionType)
              )
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('depotLang.depotCodeAndName')"
        prop="depotCodeAndName"
        align="center"
        min-width="100"
      />
      <el-table-column
        :label="$t('gmtModified')"
        prop="gmtModified"
        align="center"
        min-width="100"
      />
      <el-table-column
        :label="$t('operation')"
        width="220"
        align="center"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button
            type="text"
            @click="handleEdit(row)"
          >{{ $t("edit") }}
          </el-button>

          <el-button
            v-if="row.regionStatus !== depotStatusDict.ENABLE_STATUS.value"
            type="text"
            @click="handleEnable(row)"
          >
            {{ $t("enable") }}
          </el-button>
          <el-button
            v-if="row.regionStatus === depotStatusDict.ENABLE_STATUS.value"
            type="text"
            @click="handleDisable(row)"
          >
            {{ $t("disable") }}
          </el-button>
          <el-button
            v-if="row.regionStatus === depotStatusDict.DRAFT_STATUS.value"
            type="text"
            @click="handleDelete(row)"
          >
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 新增/编辑弹层 -->
    <info-form
      v-model="regionBaseParams.id"
      :visible="regionDialog"
      :title="title"
      :operation="regionBaseParams.operation"
      @close="close"
    />
  </div>
</template>

<script>
import InfoForm from './components/info-form.vue'
import { depotStatusDict, depotRegionTypeDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import {
  getDepotList,
  getDepotListAllStatus
} from '@/api/business/depotManage/depot'
import {
  getDepotRegionPageList,
  deleteDepotRegion,
  setDepotRegionStatusEnable,
  setDepotRegionStatusDisable
} from '@/api/business/depotManage/partition'
export default {
  name: 'Region',
  components: { InfoForm },
  data() {
    return {
      depotList: [], // 仓库下拉列表
      depotListAllStatusList: [], // 仓库所有状态下拉
      total: 0,
      loading: false,
      queryParams: {
        regionCode: null,
        regionName: null,
        depotId: null,
        regionStatus: null,
        pageNo: 1,
        pageSize: 10
      },
      tableData: [], // 分区列表
      depotStatusDict: depotStatusDict,
      getTitleByValue: getTitleByValue,
      regionStatusTag: ['info', 'success', 'danger'],
      regionStatusOptions: getDictOptionList(depotStatusDict), // 分区状态
      depotRegionTypeOptions: getDictOptionList(depotRegionTypeDict), // 分区类型
      regionBaseParams: {
        id: null,
        operation: 0
      },
      regionDialog: false,
      title: ''
    }
  },
  created() {
    this.getDepotList()
    this.getDepotRegionPageList()
    this.getDepotListAllStatus()
  },
  methods: {
    // 所有状态下拉列表
    async getDepotListAllStatus() {
      const { data } = await getDepotListAllStatus()
      this.depotListAllStatusList = data
    },
    // 仓库下拉列表
    async getDepotList() {
      const { data } = await getDepotList()
      this.depotList = data
    },
    // 重置
    async handleReset() {
      await this.$refs.queryParams.resetFields()
      this.getDepotRegionPageList()
    },
    // 查询
    async handleQuery() {
      this.queryParams.pageNo = 1
      this.getDepotRegionPageList()
    },
    // 获取分区列表
    async getDepotRegionPageList() {
      this.loading = true
      const res = await getDepotRegionPageList({ ...this.queryParams })
      console.log(res)
      this.loading = false
      this.tableData = res.data.resultList
      this.total = res.data.totalRowNum
    },
    // 新增
    async handleAdd() {
      this.regionBaseParams.operation = 1
      this.title = this.$t('add')
      this.regionDialog = true
    },
    async handleSubmit() {
      this.$refs.queryParams.submit()
    },
    // 编辑
    async handleEdit(row) {
      this.regionBaseParams.operation = 2
      this.regionBaseParams.id = row.regionId
      this.id = row.regionId
      this.title = this.$t('edit')
      setTimeout(() => {
        this.regionDialog = true
      }, 400)
    },
    handleOpenInfo(row) {
      this.regionBaseParams.operation = 0
      this.regionBaseParams.locationId = row.regionId
      this.title = this.$t('info')
      setTimeout(() => {
        this.regionDialog = true
      }, 400)
    },
    // 删除
    async handleDelete(row) {
      console.log(row.depotId)
      const flag = await this.$confirm(
        this.$t('delete') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      console.log('取消')
      const res = await deleteDepotRegion({
        regionId: row.regionId,
        modifiedUser: 0
      })
      if (!res || res.returnCode !== 10000) return
      this.$message({
        message: this.$t('success'),
        type: 'success'
      })
      this.getDepotRegionPageList()
    },
    // 启用
    async handleEnable(row) {
      const flag = await this.$confirm(
        this.$t('enable') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotRegionStatusEnable({
        regionId: row.regionId,
        modifiedUser: 0
      })
      if (!res || res.returnCode !== 10000) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getDepotRegionPageList()
    },
    // 禁用
    async handleDisable(row) {
      const flag = await this.$confirm(
        this.$t('diasble') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotRegionStatusDisable({
        regionId: row.regionId,
        modifiedUser: 0
      })
      if (!res || res.returnCode !== 10000) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getDepotRegionPageList()
    },
    // 关闭弹出层
    close() {
      this.regionBaseParams.id = null
      this.regionBaseParams.operation = 0
      this.regionDialog = false
      this.getDepotRegionPageList()
    },
    // 切换页码
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getDepotRegionPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>

<style scss="lang" scoped></style>
